import Vue from 'vue'
import Router from 'vue-router'
//用懒加载的方式加载页面
const Login = () => import('../pages/Login/Login')
const Home = () => import('../pages/Home/Home')
const Welcome = () => import('../pages/Home/children/Welcome')
const HallManage = () => import('../pages/Home/children/HallManage')
const UserManage = () => import('../pages/Home/children/UserManage')
const MovieManage = () => import('../pages/Home/children/MovieManage')
const MovieSchedule = () => import('../pages/Home/children/MovieSchedule')
const CinemaManage = () => import('../pages/Home/children/CinemaManage')
const CommentManage = () => import('../pages/Home/children/CommentManage')
const OrderManage = () => import('../pages/Home/children/OrderManage')
const MySessage = () => import('../pages/Home/children/MySessage')
Vue.use(Router);

const routes = [{
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    children: [{
        path: 'welcome',
        component: Welcome
      }, {
        path: 'user_manage',
        component: UserManage
      },
      {
        path: 'movie_manage',
        component: MovieManage
      },
      {
        path: 'movie_schedule',
        component: MovieSchedule
      },
      {
        path: 'cinema_manage',
        component: CinemaManage
      },
      {
        path: 'hall_manage',
        component: HallManage
      },
      {
        path: 'comment_manage',
        component: CommentManage
      },
      {
        path: 'order_manage',
        component: OrderManage
      },
      {
        path: 'my_message',
        component: MySessage
      },
      {
        path: '/home',
        redirect: '/home/welcome'
      }
    ]
  }
]
const router = new Router({
  routes
})

//标题 全局守卫
router.beforeEach((to, from, next) => {
  // 从from跳转到to
  // document.title = to.matched[0].meta.title;
  // to将要访问的路径
  // from 代表从哪个路径跳转而来 
  // next 是一个函数，表示放行
  // next()放行 next( ' /login ')强制跳转
  if (to.path === "/login") return next()
  //获取tokenI
  const session = window.sessionStorage.getItem('token')
  if (!session) return next("/login")
  next()
})
export default router
